package com.gmu.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gmu.domain.ResResult;
import com.gmu.pojo.SampleUnitsActivities;
import com.gmu.service.SampleUnitsActivitiesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 采样单位活动表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@Api(tags = "采样单位活动API")
@RestController
@RequestMapping("/gmu/sample-units-activities")
public class SampleUnitsActivitiesController {
    @Autowired
    private SampleUnitsActivitiesService sampleUnitsActivitiesService;

    /**
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    @ApiOperation("获取该id的采样单位活动")
    public ResResult<?> getById(@PathVariable Integer id) {
        SampleUnitsActivities activities = sampleUnitsActivitiesService.getById(id);
        if (activities == null) {
            return new ResResult<>(201, "获取失败", null);
        } else {
            return new ResResult<>(200, "获取成功", activities);
        }
    }

    /**
     * @return
     */
    @GetMapping("/getAll")
    @ApiOperation("获取所有的采样单位活动")
    public ResResult<?> getAll() {
        List<SampleUnitsActivities> activities = sampleUnitsActivitiesService.list();
        if (activities == null || activities.size() == 0) {
            return new ResResult<>(201, "获取失败", null);
        } else {
            return new ResResult<>(200, "获取成功", activities);
        }
    }

    /**
     * @param sampleUnitsActivities
     * @return
     */
    @PostMapping("/addSampleUnitsActivities")
    @ApiOperation("添加采样活动")
    public ResResult<?> addSampleUnitsActivities(SampleUnitsActivities sampleUnitsActivities) {
        boolean save = sampleUnitsActivitiesService.save(sampleUnitsActivities);
        return save ? new ResResult(200, "添加成功") : new ResResult(201, "添加失败");

    }

    /**
     * @param sampleUnitsActivities
     * @return
     */
    @PutMapping("/updateSampleUnitsActivities")
    @ApiOperation("修改采样活动")
    public ResResult<?> updateSampleUnitsActivities(SampleUnitsActivities sampleUnitsActivities) {
        boolean update = sampleUnitsActivitiesService.updateById(sampleUnitsActivities);
        return update ? new ResResult(200, "修改成功") : new ResResult(201, "修改失败");
    }

    /**
     * @param list
     * @return
     */
    @DeleteMapping("/deleteSampleUnitsActivities")
    @ApiOperation("删除采样活动")
    public ResResult<?> deleteSampleUnitsActivities(List<Integer> list) {
        boolean remove = sampleUnitsActivitiesService.removeByIds(list);
        return remove ? new ResResult(200, "批量删除成功") : new ResResult(201, "批量删除失败");
    }

    @GetMapping("/getPage/{currentPage}/{pageSize}")
    @ApiOperation("分页查询")
    public ResResult<?> getPage(@PathVariable int currentPage,@PathVariable int pageSize) {
        IPage<SampleUnitsActivities> pages = sampleUnitsActivitiesService.getPages(currentPage, pageSize);
        if (currentPage > (int) pages.getPages()) {
            pages = sampleUnitsActivitiesService.getPages((int) pages.getPages(), pageSize);
        }
        if (pages == null) {
            return new ResResult(201, "数据有误,", null);
        } else {
            return new ResResult(200, "分页成功", pages);
        }
    }
}

